package com.coffee.test.server;

import cn.hutool.core.util.NumberUtil;
import com.coffee.netty.CommandHandler;
import com.coffee.netty.util.ExecutorManager;
import com.coffee.test.util.SendMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import lombok.AllArgsConstructor;

import java.util.concurrent.Executor;

/**
 * @author songkui
 * @since 2024/8/5 16:19
 */
@AllArgsConstructor
public class HjServerHandler implements CommandHandler {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(HjServerHandler.class);
    @Override
    public void handleCommand(ChannelHandlerContext ctx, Object msg) throws Exception {

        if (null == msg){
            logger.error("Channel msg >>> msg is null;  channel id: {}", ctx.channel().id());
            return;
        }
        logger.info("Channel msg >>> channel id: {};  msg: {}", ctx.channel().id(), msg);
        String m = (String) msg;
        if ("ping".equals(m)){
            return;
        }

        int mg = NumberUtil.parseInt(m, 0);
        if (mg > 0){
            SendMessage.sendTestData(ctx.channel(), m);
        }
//        Executor executor = ExecutorManager.getInstance().getDisruptor(ctx.channel().id().hashCode());
//        executor = null == executor ? ExecutorManager.getInstance().getDefaultExecutor() : executor;
//        executor.execute(() -> {
//            System.out.println("---Do msg :" + msg);
//            logger.info("DOOO Channel msg >>> channel id: {};  msg: {}", ctx.channel().id(), msg);
//        });

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("ChannelInactive connection Illegal"+ ctx.channel().toString());
        logger.info("ChannelInactive connection Illegal {}", ctx.channel().toString());
    }
}
